package work.konbai.bmc.cms.controller;

import work.konbai.bmc.cms.domain.MaterialDO;
import work.konbai.bmc.cms.dto.MaterialDTO;
import work.konbai.bmc.cms.service.MaterialService;
import work.konbai.bmc.cms.vo.MaterialVO;
import work.konbai.bmc.common.annotation.Log;
import work.konbai.bmc.common.config.BootdoConfig;
import work.konbai.bmc.common.controller.BaseController;
import work.konbai.bmc.common.service.FileService;
import work.konbai.bmc.common.utils.FileUtil;
import work.konbai.bmc.common.utils.PageUtils;
import work.konbai.bmc.common.utils.Query;
import work.konbai.bmc.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 物资管理
 *
 * @author KonBAI
 * @version 1.0
 * Create by 2020/02/07 22:12
 */
@Controller
@RequestMapping("/cms/material")
public class MaterialController extends BaseController {
    String prefix = "cms/material";

    @Autowired
    private MaterialService materialService;

    @Autowired
    private FileService sysFileService;

    @Autowired
    private BootdoConfig bootdoConfig;

    /**
     * 跳转到物资管理页面
     * @return
     */
    @GetMapping()
    @RequiresPermissions("cms:material:material")
    String Material() {
        return prefix + "/material";
    }

    /**
     * 分页查询所有物资信息
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);
        List<MaterialVO> materialList = materialService.list(query);
        int total = materialService.count(query);
        PageUtils pageUtils = new PageUtils(materialList, total);
        return pageUtils;
    }

    /**
     * 查询所有物资信息
     * @return
     */
    @ResponseBody
    @GetMapping("/listMaterial")
    public List<MaterialDO> materialList() {
        return materialService.listMaterial();
    }

    /**
     * 添加物资信息页面
     * @return
     */
    @GetMapping("/add")
    @RequiresPermissions("cms:material:add")
    String add() {
        return prefix + "/add";
    }

    /**
     * 修改物资信息页面
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("cms:material:edit")
    String edit(@PathVariable("id") Long id, Model model) {
        MaterialVO material = materialService.get(id);
        model.addAttribute("material", material);
        return prefix + "/edit";
    }

    /**
     * 保存物资信息
     */
    @Log("保存物资信息")
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("cms:material:add")
    public R save(MaterialDO material) {
        material.setCreateUser(getUser().getName());
        if (materialService.save(material) > 0) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 修改物资信息
     */
    @Log("修改物资信息")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("cms:material:edit")
    public R update(MaterialDO material) {
        material.setUpdateUser(getUser().getName());
        if (materialService.update(material) > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 删除物资信息
     */
    @Log("删除物资信息")
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("cms:material:remove")
    public R remove(MaterialDO materialDO) {

        if (materialService.remove(materialDO.getId()) > 0) {
            if (sysFileService.remove(materialDO.getImgId()) > 0) {
                boolean b = false;
                if (null != materialDO.getImgId()) {
                    String fileName = bootdoConfig.getUploadPath()
                            + sysFileService.get(materialDO.getImgId())
                            .getUrl().replace("/files/", "");
                    b = FileUtil.deleteFile(fileName);
                }
                if (!b) {
                    return R.error("数据库记录删除成功，没有可删除图片");
                }
                return R.ok();
            } else {
                return R.error("数据库记录删除成功，文件删除失败");
            }
        }
        return R.error();
    }

    /**
     * 打开转交报表
     *
     * @return
     */
    @GetMapping("/transfer/{id}")
    @RequiresPermissions("cms:material:transfer")
    String addTransfer(@PathVariable("id") Long id, Model model) {
        model.addAttribute("materialId", id);
        return prefix + "/transfer";
    }

    /**
     * 保存转交记录
     *
     * @return
     */
    @Log("保存转交记录")
    @ResponseBody
    @PostMapping("/transfer/save")
    @RequiresPermissions("cms:material:transfer")
    R saveTransfer(MaterialDTO materialDTO) {
        materialDTO.setCreateUser(getUser().getName());
        if (materialService.saveTransfer(materialDTO) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 物资详情(记录)
     *
     * @return
     */
    @GetMapping("/details/{id}")
    // @RequiresPermissions("cms:material:details")
    String details(@PathVariable("id") Long id, Model model) {
        MaterialVO material = materialService.get(id);
        List<MaterialDTO> materialDTOList = materialService.details(id);
        model.addAttribute("material", material);
        model.addAttribute("transferList", materialDTOList);
        return prefix + "/details";
    }

    @PostMapping("/details/list")
    R detailsList() {
        materialService.details(1L);
        return R.ok();
    }
}
